% /*******************************************************************************
%  * Hybrid Correction Method : constant calculation
%  * *****************************************************************************
%  * Compute the Hybrid Correction Constant (Stine03), for more detail, see
%  * @text(Stine04 p.76). In summary, introduce p, the percentage of VC to use
%  * for correction.
%  * 
%  * @param a     Multiplicand
%  * @param b     Multiplier
%  * @param m     Multiplicand Width
%  * @param n     Multiplier Width
%  * @param r     Product Width
%  * @param k     Extra bits to keep
%  * @param q
%  * @param nbitsvar      
%  * @return      HC Product
%  */
function cor_tot = hcm_const(a,b,m,n,r,k,q,nbitsvar)
err_red = 0;
for t = r + k + 1 : m + n
    err_red = err_red + (m+n+1-t) * 2^(-t);
end
var_cor = 0.25 * nbitsvar * 2 ^(-r-k);
err_rnd = 2^(-r) * (1 - 2^(-k));
err_tot = 0.25 * err_red + 0.5 * err_rnd - var_cor;
cor_tot = rnd_near(err_tot, r+k);

j = m+n-r-k-1;
if (n < r + k)
    b_start = n;
else
    b_start = r+k-1;
end
one_count = 0;
a_int = uint32(a * (2^m));
b_int = uint32(b * (2^n));
for t = n-b_start+1:n-b_start+nbitsvar
    if (bitand(uint32(2 ^ t),b_int) && bitand(uint32(2 ^ (j - t)),a_int))
        one_count = one_count + 1;
    end
end
cor_tot = cor_tot + one_count * 2 ^ (-r-k);
end
